<template>
    <div class="container bag">
        <div class="nav">
            <div class="topTitle">
                <div class="pric" @click="goHome"></div>
                <p>{{localStorages}}</p>
            </div>

            <div class="nav_top">
                
                <div class="last">
                    <van-dropdown-menu active-color="#ff6600">
                        <van-dropdown-item v-model="value1" :options="option1" @change="open"/>
                        <van-dropdown-item v-model="value2" :options="option2" />
                    </van-dropdown-menu>
                </div>
            </div>

            <div class="catianer">
                <ul>
                    <li :class="IndexData == 0 ? 'con' : ''" @click="IndexData = 0">最新发布</li>
                    <li :class="IndexData == 1 ? 'con' : ''" @click="IndexData = 1">玩家出售</li>
                    <li :class="IndexData == 2 ? 'con' : ''" @click="IndexData = 2">签约卖家</li>
                </ul>
            </div>
        </div>

        
        <div class="listsq" v-if="listData">
            <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
               
                <ul>
                    <li v-for="(item, index) in items" :key="index">
                        <div class="leftq">
                            <div class="img">
                                <img ref="img" @click="imgs(index)" :src="item.imgs" alt="">
                                <div class="floats">1/1</div>
                            </div>

                        </div>

                        <div class="right" @click="next(item.id)">
                            <div class="title">{{ item.title }}</div>
                            <div class="symb">{{ item.operatorName }}/{{ item.gameServerName }}</div>
                            <div class="price"><span>￥</span>{{ item.price }}</div>
                        </div>
                    </li>
                </ul>
            </van-list>
        </div>
    </div>
</template>

<script>
import { getuserobj, getlistData } from "../assets/api/home.js";
import { ImagePreview } from 'vant';
export default {
    data() {
        return {
            localStorages:"",
            title:"",
            list: [],
            loading: false,
            finished: false,
            username: null,
            IndexData: 0,
            items:null,
            value1: "服务器",
            value2: 'a',

            option1: [
                { text: '服务器', value: "服务器" },
                { text: '苹果', value: '苹果' },
                { text: '安卓', value: '安卓' },
            ],
            option2: [
                { text: '保障服务', value: 'a' },
                { text: '安全保障服务', value: 'b' },
                { text: '找回包赔', value: 'c' },
                { text: '无限', value: 'd' },
            ],

            listData: null,
        };
    },
    methods: {
        //获取顶部tab的数据
        getuserobjFun() {
            getuserobj().then((data) => {
                console.log(data.data.Data);
                this.username = data.data.Data
            })
        },
        getvalue(){
            console.log(1);
        },
        //获取列表的数据
        getlistDataFun() {
            getlistData().then((data) => {
                this.listData = data.data.Data.hits
                this.items = this.listData
                console.log(this.listData);
            })
        },

        //tab选项卡
        getIndex(index) {
            this.IndexNum = index
            console.log(this.IndexNum);
        },
        goHome(){
            this.$router.go(-1)
        },

        //点击图片放大预览
        imgs(id) {
            console.log(this.$refs.img[id].currentSrc);
            ImagePreview({
                // currentSrc
                images: [
                    this.$refs.img[id].currentSrc
                ],
                closeable: true,
                closeIconPosition: 'top-left',
            });
        },
        next(id){
            var obj = this.listData.find((obj)=>obj.id === id);
            if(obj == -1){
                console.log("errey");
            }else{
                window.localStorage.setItem("obj",JSON.stringify(obj))
            }
            this.$router.push('/trade')
        },
        onLoad() {
            setTimeout(() => {
                for (let i = 0; i < 5; i++) {
                    this.listData.push({
                        id:this.listData[i].id,
                        imgs: this.listData[i].imgs,
                        title: this.listData[i].title,
                        price: this.listData[i].price,
                        operatorName: this.listData[i].operatorName,
                        gameServerName: this.listData[i].gameServerName
                    });
                }

                // 加载状态结束
                this.loading = false;

                // 数据全部加载完成
                if (this.listData.length >= 40) {
                    this.finished = true;
                }
            }, 1000);

        },

        open(value){
            console.log(value);
        this.items= this.listData.filter((data)=>{
                if(data.gamePlatformName == value){
                    return data
                }else if(value =="服务器"){
                    return this.listData
                }
            })
        }

    },

    created() {
        this.getuserobjFun()
        this.getlistDataFun()
        this.localStorages = window.localStorage.getItem("name")
        console.log(this.localStorages);
        this.title = this.$route.query.titel
        // console.log(this.title[0].name);
    }
}
</script>

<style lang="scss">
@import "../assets/css/base.css";

.bag {
    background-color: #f1f1f1;
    height: 100%;
}

.nav {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    margin: 0 auto;
    z-index: 999;
    background-color: #fff;
    .topTitle {
        height: 47px;
        background-color: #f6f6f6;
        display: flex;
        padding: 0 15px 0 15px;

        p {
            height: 47px;
            line-height: 47px;
            color: #1f2023;
            font-size: 16px;
            flex: 1;
            text-align: center;

        }

        .pric {
            width: 37px;
            background: url(../assets/img/left.png) no-repeat center center;
            background-size: 25%;
        }

        .pric:last-child {
            width: 37px;
            background: url(../assets/img/switch-ico.png) no-repeat center center;
            background-size: 50%;
        }
    }

    .nav_top {
        width: 100%;

        ul {
            height: 37px;
            line-height: 37px;
            background-color: #ffffff;
            border-top: 1px solid #eaeaea;
            border-bottom: 1px solid #eaeaea;
            padding: 0px 14px;
            display: flex;

            li {
                margin-right: 23px;
                font-size: 14px;
                font-weight: 500;
                color: #939393;

                &.con {
                    color: #ff6007;
                }

            }
        }
    }

    .catianer {
        ul {
            display: flex;
            border-bottom: 2px solid #f3f3f4;
            background-color: #ffffff;

            li {
                width: 33.3%;
                text-align: center;
                height: 36px;
                line-height: 36px;
                font-size: 15px;
                color: #aaacad;

                &.con {
                    border-bottom: 2px solid #fd7421;
                    color: #fd7421;
                }

            }
        }
    }
}

.listsq {
    padding-top: 128px;
    ul {
        li {
            display: flex;
            background-color: #fff;
            margin-top: 10px;

            .leftq {
                width: 97px;
                height: 140px;

                padding: 14px 14px 12px 14px;

                .img {
                    width: 97px;
                    height: 97px;
                    position: relative;
                    border: 1px solid #d2d2d2;

                    img {
                        height: 100%;
                    }
                }

                .floats {
                    position: absolute;
                    bottom: 5px;
                    right: 6px;
                    width: 40px;
                    height: 26px;
                    color: #fff;
                    font-size: 10px;
                    font-weight: 500;
                    line-height: 26px;
                    text-align: center;
                    background-color: rgba(0, 0, 0, 0.5);
                }
            }

            .right {
                padding: 14px 14px 12px 14px;

                .title {
                    color: #0e0206;
                    font-size: 18px;
                    font-weight: 700;
                }

                .symb {
                    margin-top: 5px;
                    font-size: 13px;
                    height: 16px;
                    line-height: 16px;
                    color: #808080;
                }

                .price {
                    color: #ff7729;
                    font-size: 16px;
                    font-weight: 700;
                }
            }
        }
    }
}
</style>